package org.ricks.action;

import org.ricks.action.proto.common.Common;
import org.ricks.action.proto.player.Player;
import org.ricks.ioc.anno.Action;
import org.ricks.ioc.anno.Autowired;
import org.ricks.ioc.event.anno.EventReceiver;
import org.ricks.ioc.utils.Logger;
import org.ricks.net.ActionErrorEnum;
import org.ricks.net.action.ActionExceptionEvent;
import org.ricks.net.action.context.head.DefHeadMeta;
import org.ricks.net.action.context.FlowContext;
import org.ricks.net.action.anno.ActionMethod;
import org.ricks.net.action.context.head.HeadMeta;
import org.ricks.net.http.service.UserService;

import java.io.IOException;
import java.nio.ByteBuffer;

@Action
public class UserAction {

    public static void main(String[] args) {
        String name = "Alice";
        int age = 30;
        String info = "姓名：${name}，年龄：${age + 5}"; // 直接支持表达式计算
        System.err.println(info);

        // 编译错误：布尔值无法直接插入字符串
        String error = "状态：${true}";
        System.err.println(error);

    }

    @Autowired
    private UserService userService;


    @EventReceiver
    public void actionExceptionEvent(ActionExceptionEvent<? extends HeadMeta> actionExceptionEvent) {
        Throwable throwable = actionExceptionEvent.getThrowable();
        FlowContext<? extends HeadMeta> flowContext = actionExceptionEvent.getContext();
        HeadMeta headMeta = flowContext.headMeta;

        if (throwable instanceof MsgException msgException) {
            System.err.println("业务异常 -- " + msgException);
            if (headMeta.isRpc()) {
                // RPC请求：使用rpcError响应
                flowContext.rpcError(msgException.getMsgCode(), msgException.getMessage());
            } else {
                // 普通请求：使用错误cmd=10000响应，并在消息体中包含原始cmd
                Common.RickErrorPush errorPush = Common.RickErrorPush.newBuilder()
                        .setCmd(msgException.getMsgCode())
                        .setMsg(msgException.getMessage())
                        .build();

                // 创建错误HeadMeta，cmd固定为10000
                HeadMeta errorHeadMeta = new DefHeadMeta((short) 10000);


                // 发送消息
                flowContext.writeBytes(errorHeadMeta, errorPush.toByteArray());
            }
            return;
        }

        Logger.error("内部异常", throwable);
    }
    private Common.ErrorPush buildError (int error) {
        return Common.ErrorPush.newBuilder().setError(error).build();
    }

    @ActionMethod(command = 1)
    public void test(FlowContext<DefHeadMeta> context) {
        ActionErrorEnum.itemNumErr.assertTrue(1==2);
//        throw new MsgException((short) 99,"测试断言机制");
//        if(context instanceof WsFlowContext webSocketFlowContext) {
//            Login.LoginRequest data = context.deserialize(Login.LoginRequest.class);
//            String s = userService.test();
//            // 优化后：直接通过 TypeReference 传递泛型类型
//            Logger.info(System.currentTimeMillis() + " -- websocket协议 - 进入测试代码！@  ---- " + data.getUsername() + " " +
//                    "-----  " + s  + "  ------- " + data.getDid());
//            Login.LoginResponse.Builder builder = Login.LoginResponse.newBuilder();
//            builder.setPlayerInfo(buildPlayerInfo());
//            builder.setRpcId(1);
//            context.write(builder.build().toByteArray());
//        } else if (context instanceof DefContext tcpFlowContext) {
//            String data = context.deserialize(String.class);
//            String s = userService.test();
//            Logger.info("TCP协议 - 进入测试代码！@  ---- " + data + " -----  " + s);
//        }
    }

    private Player.PlayerInfo buildPlayerInfo () {
        Player.PlayerInfo.Builder builder = Player.PlayerInfo.newBuilder();
        builder.setAccount("i am demo . upi are god ?");
        builder.setLv(999);
        builder.setName("fuck you  王老师！");
        builder.setExp(1);
        return builder.build();
    }

}
